global SSDIMed "/disk/agedisk4/medicare.work/miller-DUA50377/proj_ssdi"
* Settings
version 16
do "$SSDIMed/scripts/_auxiliary/_project_settings.do"

* Force redo of estimation?
*   redo_estimates = 0: use existing estimates, if available
*   redo_estimates = 1: redo estimation, even if existing estimates are available
local redo_estimates 0

* Sample definitions:
* - main (all ages, all years)
local sample_main 1
* - ages 22-62
local sample_2262 inrange(age_year_covstart_fill, 22, 62)
* - ages 51-52
local sample_5152 inrange(age_year_covstart_fill, 51, 52)
* - first full year
local sample_ffy (rfrnc_yr == init_yr + 1)


* -------------------------------------------------------------------------------------------------
* Part 1. Relate DI entrant outcomes to initial conditions at time of entry
* -------------------------------------------------------------------------------------------------

* DI entrant outcomes, by x, where x = 
*   i.covstart_year              = calendar year of Medicare entry
*   c.unemp_rate_county_atapp    = unemployment rate at time of application
*   i.unemp_rate_q20_atapp       = ventiles of unemp_rate_county_atapp
*   i.age_year_covstart_fill     = age in years at Medicare entry


* -------------------------------------------------------------------------------------------------
* Load data
use "$SSDIMed/data/analysis/bene-year_outcomes_sample-main.dta",  clear
assert dib_sample == 1
qui sum age_year_covstart_fill
assert r(min)==20 & r(max)==62

* Which controls to include in each regression
* + Spec 2:   years enrolled by county (baseline)
local controls_02 i.years_since_covstart##i.fipscounty_firstnm_g

* Estimation
local sample 2262
local ctrl 02
local clust fipscounty_firstnm

foreach y in tot_pmt died_adj {
foreach x in c.unemp_rate_county_atapp i.unemp_rate_q20_atapp {
  
  * Check if estimation results already exist, do not recompute unless needed
  local results_stub $SSDIMed/results/estimates/x-`x'/x-`x'_y-`y'_controls-`ctrl'_cluster-`clust'_sample-`sample'
  capture confirm file "`results_stub'.dta"
  if max(_rc, `redo_estimates') == 0 {
    di _newline "Using existing estimation results in `results_stub'.dta. Set redo_estimates=1 to recalculate results."
    continue
  }
  
  * Estimation
  local reg_cmd reghdfe `y' `x' if `sample_`sample'', abs(`controls_`ctrl'') cluster(`clust')
  noisily di "`reg_cmd'"
  `reg_cmd'
  
  * Additional estimation stats
  *   Dep var mean
  sum `y' if e(sample)
  local y_mean_esample = r(mean)
  *   Number of unique beneficiaries in esample
  qui glevelsof bene_id if e(sample), nolocal
  local N_benes_esample = r(J)
  
  * Save results
  cap mkdir "$SSDIMed/results/estimates/x-`x'"
  estimates save "`results_stub'.ster", replace
  local addlabel addlabel(y, `y', y_mean_esample, `y_mean_esample', x, `x', controls, `controls_`ctrl'', sample, `sample', cluster, `clust', N_benes_esample, `N_benes_esample')
  regsave using  "`results_stub'.dta", cmdline p ci detail(all) `addlabel' replace

}
}

* -------------------------------------------------------------------------------------------------
* Outcomes by unemployment interacted with 51 vs. 52 years old at entry

* Load data
use "$SSDIMed/data/analysis/bene-year_outcomes_sample-main.dta",  clear
assert dib_sample == 1
qui sum age_year_covstart_fill
assert r(min)==20 & r(max)==62

local sample 5152
assert `sample_5152' == inrange(age_year_covstart_fill, 51, 52)
keep if `sample_5152'
gen age52 = (age_year_covstart_fill == 52)
sum unemp_rate_county_atapp
gen UR = unemp_rate_county_atapp-`r(mean)'
gen age52xUR=age52*UR

* Which controls to include in each regression
* + Spec 2:   years enrolled by county (baseline)
local controls_02 i.years_since_covstart##i.fipscounty_firstnm_g

* Estimation
local ctrl 02
local clust fipscounty_firstnm

foreach y in tot_pmt died_adj {
  noisily di "Working on y = `y' and ctrl = `ctrl'"
  
  * Check if estimation results already exist, do not recompute unless needed
  local results_stub $SSDIMed/results/estimates/x-age52xUR/x-age52xUR_y-`y'_controls-`ctrl'_cluster-`clust'_sample-`sample'
  capture confirm file "`results_stub'.dta"
  if max(_rc, `redo_estimates') == 0 {
    di _newline "Using existing estimation results in `results_stub'.dta. Set redo_estimates=1 to recalculate results."
    continue
  }
  
  * Estimation
  reghdfe `y' age52 UR age52xUR, abs(`controls_`ctrl'') cluster(`clust')
  sum `y' if e(sample)
  local y_mean_esample = r(mean)
  
  * Save results
  cap mkdir "$SSDIMed/results/estimates/x-age52xUR"
  estimates save "`results_stub'.ster", replace
  regsave using  "`results_stub'.dta", cmdline p ci detail(all) addlabel(y, `y', y_mean_esample, `y_mean_esample', x, "age52 UR age52xUR", controls, `controls_`ctrl'', sample, `sample', cluster, `clust') replace
}



* -------------------------------------------------------------------------------------------------
* Part 2. Relate DI entry to initial conditions at time of entry
* -------------------------------------------------------------------------------------------------

* -------------------------------------------------------------------------------------------------
* Incidence: average across all ages
use "$SSDIMed/data/analysis/county-month-age_entry_sample-main.dta", clear
qui sum age_year_covstart_fill
assert r(min)==20 & r(max)==62

* Which controls to include in each regression
local controls_02 i.fipscounty_firstnm_g

* Estimation
local sample 2262
local y incidence_pop_age_atapp
local w pop_age_atapp
local ctrl 02
local clust fipscounty_firstnm

foreach x in c.unemp_rate_county_atapp i.unemp_rate_q20_atapp {
  
  * Check if estimation results already exist, do not recompute unless needed
  local results_stub $SSDIMed/results/estimates/x-`x'/x-`x'_y-`y'_controls-`ctrl'_cluster-`clust'_sample-`sample'
  capture confirm file "`results_stub'.dta"
  if max(_rc, `redo_estimates') == 0 {
    di _newline "Using existing estimation results in `results_stub'.dta. Set redo_estimates=1 to recalculate results."
    continue
  }
  
  * Estimation
  if "`controls_`ctrl''" == "none" regress `y' `x' [aw = `w'] if `sample_`sample'', cluster(`clust')
  else                             reghdfe `y' `x' [aw = `w'] if `sample_`sample'', abs(`controls_`ctrl'') cluster(`clust')
  sum `y' if e(sample) [aw = `w']
  local y_mean_esample = r(mean)
  
  * Save results
  cap mkdir "$SSDIMed/results/estimates/x-`x'"
  estimates save "`results_stub'.ster", replace
  regsave using  "`results_stub'.dta", cmdline p ci detail(all) addlabel(y, `y', y_mean_esample, `y_mean_esample', x, `x', controls, `controls_`ctrl'', sample, `sample', cluster, `clust') replace
}


* -------------------------------------------------------------------------------------------------
* Incidence by unemployment interacted with 51 vs. 52 years old at entry
use "$SSDIMed/data/analysis/county-month-age_entry_sample-main.dta", clear
qui sum age_year_covstart_fill
assert r(min)==20 & r(max)==62

local sample 5152
assert `sample_5152' == inrange(age_year_covstart_fill, 51, 52)
keep if `sample_5152'
gen age52 = (age_year_covstart_fill == 52)
sum unemp_rate_county_atapp [aw = pop_19_61_atapp]
gen UR=unemp_rate_county_atapp-`r(mean)'
gen age52xUR=age52*UR

* Which controls to include in each regression
local controls_02 i.fipscounty_firstnm_g

* Estimation
local sample 5152
local y incidence_pop_age_atapp
local x c.unemp_rate_county_atapp
local w pop_age_atapp
local ctrl 02
local clust fipscounty_firstnm

if 1 {

  * Check if estimation results already exist, do not recompute unless needed
  local results_stub $SSDIMed/results/estimates/x-age52xUR/x-age52xUR_y-`y'_controls-`ctrl'_cluster-`clust'_sample-`sample'
  capture confirm file "`results_stub'.dta"
  if max(_rc, `redo_estimates') == 0 {
    di _newline "Using existing estimation results in `results_stub'.dta. Set redo_estimates=1 to recalculate results."
    continue
  }
  
  * Estimation
  if "`controls_`ctrl''" == "none" regress `y' age52 UR age52xUR [aw = `w'], cluster(`clust')
  else                             reghdfe `y' age52 UR age52xUR [aw = `w'], abs(`controls_`ctrl'') cluster(`clust')
  sum `y' if e(sample) [aw = `w']
  local y_mean_esample = r(mean)
  
  * Save results
  cap mkdir "$SSDIMed/results/estimates/x-age52xUR"
  estimates save "`results_stub'.ster", replace
  regsave using  "`results_stub'.dta", cmdline p ci detail(all) addlabel(y, `y', y_mean_esample, `y_mean_esample', x, "age52 UR age52xUR", controls, `controls_`ctrl'', sample, `sample', cluster, `clust') replace
}

